﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.ContentManagement;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using Utils.Drawing;

namespace Utils.Debugging
{
    public class Profiler
    {
        const int EvaluatePeriod = 60;

        static Profiler instance;

        protected SpriteFont font;
        Color color;
        Vector2 pos;
        float height;
        float valuesOffset;
        int frames;
        int drawFrames;
        List<ProfilerItem> profiles;
        ProfilerItem current;
        TextField txtHeadline;

        public static Profiler DEFAULT
        {
            get
            {
                if (instance == null)
                {
                    instance = new Profiler();
                }
                return instance;
            }
        }

        [Conditional("PROFILING")]
        public virtual void Init(ContentLoader loader, Vector2 pos, float valuesOffset, float height, Color color)
        {
            font = loader.Load<SpriteFont>("Fonts/Debugging");
            profiles = new List<ProfilerItem>();

            this.pos = pos;
            this.height = height;
            this.valuesOffset = valuesOffset;
            this.color = color;

            txtHeadline = new TextField(font, 64);
            txtHeadline.Text.Set("Profiler       Time(s)/60Frames  -  %ofTotal  -  %ofParent");
            txtHeadline.Origin = TextField.ORIGIN_TOP_LEFT;
            txtHeadline.Pos = pos;
        }

        [Conditional("PROFILING")]
        public void Start(string name)
        {
            if (current == null)
            {
                foreach (ProfilerItem p in profiles)
                {
                    if (String.Equals(p.Name, name))
                    {
                        current = p;
                    }
                }

                if (current == null)
                {
                    current = new ProfilerItem(name, font, null);
                    profiles.Add(current);
                }
            }
            else
            {
                ProfilerItem child = current.Find(name);
                if (child == null)
                {
                    child = new ProfilerItem(name, font, current);
                }
                current = child;
            }
            current.Start();
        }

        [Conditional("PROFILING")]
        public void Start(string name, bool updateByDraw)
        {
            Start(name);
            current.UpdateByDraw = updateByDraw;
        }

        [Conditional("PROFILING")]
        public void Stop(string name)
        {
            Debug.Assert(current.Name == name, "Wrong Profiler.Stop() call: " + name);
            current.Stop();
            current = current.Parent;
        }

        [Conditional("PROFILING")]
        public void Update(float dTime)
        {
            frames++;
            if (frames > EvaluatePeriod)
            {
                frames = 0;
                foreach (ProfilerItem p in profiles)
                {
                    if (!p.UpdateByDraw)
                    {
                        p.Evaluate();
                        p.TotalElapsedTime = p.SafedElapsedTime;
                    }
                }
            }
        }

        [Conditional("PROFILING")]
        public void Draw(float dTime)
        {
            drawFrames++;
            if (drawFrames > EvaluatePeriod)
            {
                drawFrames = 0;
                foreach (ProfilerItem p in profiles)
                {
                    if (p.UpdateByDraw)
                    {
                        p.Evaluate();
                        p.TotalElapsedTime = p.SafedElapsedTime;
                    }
                }
            }

            SpriteRenderer renderer = SpriteRenderer.DEFAULT;
            renderer.Begin(SpriteSortMode.Immediate);
            {
                txtHeadline.Draw(renderer);

                float currentYPos = pos.Y + 13f;
                foreach (ProfilerItem p in profiles)
                {
                    currentYPos += 8f;
                    p.Draw(renderer, pos.X, ref currentYPos, valuesOffset, height);
                }
            }
            renderer.End();
        }
    }
}
